package com.winit.openapi.fna.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.SPIException;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.erp.spi.finance.FinanceCustomerAccountService;
import com.winit.erp.spi.finance.command.GetCreditLimitByBpartnerIdCommand;
import com.winit.fna.spi.customer.CustomerCreditService;
import com.winit.fna.spi.customer.command.AnewCreateCustomerCreditCommand;
import com.winit.fna.spi.customer.command.AppealCustomerCreditCommand;
import com.winit.fna.spi.customer.command.CreateCustomerCreditCommand;
import com.winit.fna.spi.customer.command.GetCustomerCreditByBpartnerIdCommand;
import com.winit.fna.spi.customer.command.GetCustomerCreditByIdCommand;
import com.winit.fna.spi.customer.vo.CustomerCredit;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * 客户额度申请 <Change to the actual description of this class>
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * BW 		1.0  		2015-9-23 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping("/fna/customerCredit")
public class CustomerCreditController extends BaseController {

    /**
     * 信用额度复审通过状态值-RR
     */
    private static final String STATUS_RR="RR".intern();
    
    @Resource
    private CustomerCreditService customerCreditService;
    
    @Resource 
    private FinanceCustomerAccountService financeCustomerAccountService;

    /**
     * 创建客户额度申请
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     * @author BW
     * @date 2015-9-23
     */
    @RequestMapping(value = "/createCustomerCredit")
    public String createCustomerCredit(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        CreateCustomerCreditCommand command = new CreateCustomerCreditCommand();
        command.setCtx(CommandContext.getContext());
        Bpartner bpartner = (Bpartner) CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            try {
                CustomerCredit cc = new CustomerCredit();
                cc = JSONObject.toJavaObject(json, CustomerCredit.class);
                cc.setWinitPartnerId(bpartner.getcBpartnerId());
                cc.setWinitAccountId(bpartner.getValue());
                command.setCustomerCredit(cc);
            } catch (SPIException e) {
                throw new SPIException("javaBean 转换错误");
            }
            customerCreditService.createCustomerCredit(command);
        } else {
            throw new SPIException("json 格式错误");
        }
        return SUCCESS;
    }

    /**
     * 获取客户额度申请
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     * @author BW
     * @date 2015-9-24
     */
    @RequestMapping(value = "/getCustomerCreditByBpartnerId")
    public String getCustomerCreditByBpartnerId(@ModelAttribute RequestMsg requestMsg,
                                                @ModelAttribute ResponseMsg responseMsg) {
        //Object obj = requestMsg.getData();
        GetCustomerCreditByBpartnerIdCommand command = new GetCustomerCreditByBpartnerIdCommand();
        command.setCtx(CommandContext.getContext());
        Bpartner bpartner = (Bpartner) CommandContext.getContext().get(ApiConstant.API_BPARTNER);
       // if (obj instanceof JSONObject) {
            command.setWinitPartnerId(bpartner.getcBpartnerId());
            CustomerCredit vo=customerCreditService.getCustomerCreditByBpartnerId(command);
            GetCreditLimitByBpartnerIdCommand creditCommand=new GetCreditLimitByBpartnerIdCommand();
            creditCommand.setbPartnerId(bpartner.getcBpartnerId());
            BigDecimal allCredit=financeCustomerAccountService.getCreditLimitByBpartnerId(creditCommand);
            if(vo!=null){
                vo.setAllCredit(allCredit);
                responseMsg.setData(vo);
            }
       // } else {
        //    throw new SPIException("json 格式错误");
       // }
         
        return SUCCESS;
    }
    
    /**
     * 获取客户额度申请分页列表
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     * @author BW
     * @date 2015-9-24
     */
    @RequestMapping(value = "/getCustCreditListByBpId")
    public String getCustomerCreditListByBpartnerId(@ModelAttribute RequestMsg requestMsg,
                                                @ModelAttribute ResponseMsg responseMsg) {
        GetCustomerCreditByBpartnerIdCommand command = new GetCustomerCreditByBpartnerIdCommand();
        command.setCtx(CommandContext.getContext());
        Bpartner bpartner = (Bpartner) CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        command.setWinitPartnerId(bpartner.getcBpartnerId());
        List<CustomerCredit> voList=customerCreditService.getCustomerCreditListByBpartnerId(command);
        GetCreditLimitByBpartnerIdCommand creditCommand=new GetCreditLimitByBpartnerIdCommand();
        creditCommand.setbPartnerId(bpartner.getcBpartnerId());
        BigDecimal allCredit=financeCustomerAccountService.getCreditLimitByBpartnerId(creditCommand);
        if(null != voList){
            for (CustomerCredit customerCredit : voList) {
                if(null == customerCredit){
                    continue;
                }
                customerCredit.setAllCredit(allCredit);
            }
            responseMsg.setData(voList);
        }
         
        return SUCCESS;
    }
    
    @RequestMapping(value = "/getCustCreditListByCreditId")
    public String getCustomerCreditListByCreditId(@ModelAttribute RequestMsg requestMsg,
                                                @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        GetCustomerCreditByIdCommand command = new GetCustomerCreditByIdCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            try {
                command = JSONObject.toJavaObject(json, GetCustomerCreditByIdCommand.class);
                command.setCtx(CommandContext.getContext());
                CustomerCredit customerCredit = customerCreditService.getCustomerCreditById(command);
                if(null != customerCredit){
                    responseMsg.setData(customerCredit);
                }
            } catch (SPIException e) {
                throw new SPIException("javaBean 转换错误");
            }
        } else {
            throw new SPIException("json 格式错误");
        }
        return SUCCESS;
    }

    /**
     * 重新提交客户额度申请
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     * @author BW
     * @date 2015-9-23
     */
    @RequestMapping(value = "/anewCreateCustomerCredit")
    public String anewCreateCustomerCredit(@ModelAttribute RequestMsg requestMsg,
                                           @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            try {
                JSONObject json = (JSONObject) obj;
                CustomerCredit cc = new CustomerCredit();
                cc = JSONObject.toJavaObject(json, CustomerCredit.class);
                Bpartner bpartner = (Bpartner) CommandContext.getContext().get(ApiConstant.API_BPARTNER);
                cc.setWinitPartnerId(bpartner.getcBpartnerId());
                // 如果申请状态为复审通过RR，就是额度到期发出重新申请，需要重新插入一条额度申请
                // 否则是审核不通过发出的重新申请，更新原有的额度申请
                if(STATUS_RR.equals(cc.getStatus())){
                    cc.setWinitAccountId(bpartner.getValue());
                    CreateCustomerCreditCommand command = new CreateCustomerCreditCommand();
                    command.setCtx(CommandContext.getContext());
                    command.setCustomerCredit(cc);
                    customerCreditService.reapplyCustomerCredit(command);
                }else{
                    AnewCreateCustomerCreditCommand command = new AnewCreateCustomerCreditCommand();
                    command.setCtx(CommandContext.getContext());
                    command.setCustomerCredit(cc);
                    customerCreditService.anewCreateCustomerCredit(command);
                }
            } catch (SPIException e) {
                throw new SPIException("javaBean 转换错误");
            }
        } else {
            throw new SPIException("json 格式错误");
        }
        return SUCCESS;
    }

    /**
     * 我要申诉客户额度申请
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     * @author BW
     * @date 2015-9-23
     */
    @RequestMapping(value = "/appealCustomerCredit")
    public String appealCustomerCredit(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        AppealCustomerCreditCommand command = new AppealCustomerCreditCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            try {
                command = JSONObject.toJavaObject(json, AppealCustomerCreditCommand.class);
                command.setCtx(CommandContext.getContext());
            } catch (SPIException e) {
                throw new SPIException("javaBean 转换错误");
            }
            customerCreditService.appealCustomerCredit(command);
        } else {
            throw new SPIException("json 格式错误");
        }
        return SUCCESS;
    }
    
    /**
     * ISP重新申请信用额度
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/reapplyIspCustomerCredit")
    public String reapplyIspCustomerCredit(@ModelAttribute RequestMsg requestMsg,
                                           @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            try {
                JSONObject json = (JSONObject) obj;
                CustomerCredit cc = new CustomerCredit();
                cc = JSONObject.toJavaObject(json, CustomerCredit.class);
                Bpartner bpartner = (Bpartner) CommandContext.getContext().get(ApiConstant.API_BPARTNER);
                cc.setWinitPartnerId(bpartner.getcBpartnerId());
                cc.setWinitAccountId(bpartner.getValue());
                CreateCustomerCreditCommand command = new CreateCustomerCreditCommand();
                command.setCtx(CommandContext.getContext());
                command.setCustomerCredit(cc);
                customerCreditService.reapplyCustomerCredit(command);
            } catch (SPIException e) {
                throw new SPIException("javaBean 转换错误");
            }
        } else {
            throw new SPIException("json 格式错误");
        }
        return SUCCESS;
    } 
}
